package com.lesso.yjy.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.yjy.sunflower.domain.repository.AttendanceRecordRepository;
import com.lesso.yjy.sunflower.infra.assembler.AttendanceRecordAssembler;
import com.lesso.yjy.sunflower.infra.mapper.AttendanceRecordMapper;
import com.lesso.yjy.sunflower.model.AttendanceRecord;
import com.lesso.yjy.sunflower.pojo.dto.AttendanceRecordDTO;
import com.lesso.yjy.sunflower.pojo.dto.AttendanceRecordQueryDTO;
import com.lesso.yjy.sunflower.pojo.vo.AttendanceRecordVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class AttendanceRecordRepositoryImpl implements AttendanceRecordRepository {
    private final AttendanceRecordMapper attendanceRecordMapper;

    public AttendanceRecordRepositoryImpl(AttendanceRecordMapper attendanceRecordMapper){
        this.attendanceRecordMapper = attendanceRecordMapper;
    }

    @Override
    public Long save(AttendanceRecord record) {
        record.setDelStatus(0);
        attendanceRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public Page<AttendanceRecordVO> getPageList(AttendanceRecordQueryDTO dto) {
        Page<AttendanceRecordVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<AttendanceRecord> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getEmployeeNo()), AttendanceRecord.EMPLOYEE_NO, dto.getEmployeeNo());
        wrapper.between("check_time", dto.getCheckTimeStart(),  dto.getCheckTimeEnd());
        List<AttendanceRecord> records = attendanceRecordMapper.selectPage(page, wrapper);
        return page.setRecords(AttendanceRecordAssembler.INSTANCE.entityListToVOList(records));
    }

    @Override
    public Page<AttendanceRecordVO> getCustomPageList(AttendanceRecordQueryDTO dto) {
        Page<AttendanceRecordVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<AttendanceRecordVO> list = attendanceRecordMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        attendanceRecordMapper.deleteById(id);
    }

    @Override
    public void updateById(AttendanceRecordDTO dto) {
        AttendanceRecord entity = AttendanceRecordAssembler.INSTANCE.dtoToEntity(dto);
        attendanceRecordMapper.updateById(entity);
    }
}
